package cn.cstp.org.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements ServletRequestAware, ServletContextAware{
	/* Third Method for receiving: Serverlet binded IoC mode (It is the container that give value )
	 * and we don't need to initiate request, session and applicaiton
	 * 
	 *  
	 * two interfaces: ServletRequestAware, ServletContextAware
	 * 
	 * 
	 * 
	 * attention: to use this method, then must initiate in the execute() method
	 */
	private String username ;
	private String password;
	// get request object
	HttpServletRequest request ;
	HttpSession session;
    ServletContext application; //equal to application
	
	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}

	public void setServletContext(ServletContext context) {
		this.application = context;
		
	}


	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}	
	
	@Override
	public String execute() throws Exception {
		if(username.trim().equalsIgnoreCase("aaa") && password.equals("aaaaaa")){			
			session = request.getSession();			
			request.setAttribute("req", "request attribute");
			session.setAttribute("ses", "session attribute");
			application.setAttribute("app", "application attribute");
			request.setAttribute("new", "test");

			return SUCCESS;
		}else{
			return ERROR;
		}
	}



	
}
